package com.limh.readers.fragment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import butterknife.BindView
import com.limh.readers.R
import android.os.Bundle
import android.support.v4.view.ViewPager
import android.util.DisplayMetrics
import android.widget.HorizontalScrollView
import android.widget.RadioButton
import android.widget.RadioGroup
import com.limh.readers.utils.LogUtil
import com.limh.readers.adapter.FragmentAdapter

/**
 * Function:首页
 * author: limh
 * time:2017/7/3
 */
class HomeFragment : BaseFragment() {
    @BindView(R.id.radio_top)
    lateinit var radioNav: RadioGroup
    @BindView(R.id.vp_radio)
    lateinit var vpRadio: ViewPager

    private lateinit var fragments: ArrayList<BaseFragment>


    val TAG = "HomeFragment"

    override fun findViews(inflater: LayoutInflater?, container: ViewGroup?): View {
        return inflater?.inflate(R.layout.fragment_home, container, false) as View
    }

    override fun initViews() {
        fragments = ArrayList()
        fragments.add(DayNewsFragment())
        fragments.add(RandomFragment())
        vpRadio.adapter = FragmentAdapter(childFragmentManager, fragments)
        vpRadio.currentItem = 0
        vpRadio.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
                if (state == 2) {
                    when (vpRadio.currentItem) {
                        0 -> radioNav.check(R.id.radio_0)
                        1 -> radioNav.check(R.id.radio_1)
                    }
                }
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {

            }
        })
        radioNav.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.radio_0 -> vpRadio.setCurrentItem(0, false)
                R.id.radio_1 -> vpRadio.setCurrentItem(1, false)
            }
        }
    }

    override fun initDatas() {

    }

    override fun onResume() {
        super.onResume()
    }

}